#pragma once

#include <QDir>
#include <QWidget>
#include <QSystemTrayIcon>
#include <QEasingCurve>

#include "ui_LxMemo.h"
#include "lucencydialog.h"
#include "Memo.h"
#include "ColorChunk.h"

#include "sqlite3.h"

class Path
{
public:
    Path(const QString& s) {
        _paths = s.split('/', Qt::SkipEmptyParts);
    }
    Path(const QStringList& p) : _paths(p) {}

    Path& operator+(const QString& d) { _paths.append(d); return *this; }
    //Path& operator+=(const Path& p) { _paths.append("/" + p.path()); return *this; }
    Path& operator+=(const QString& p) { _paths.append(p); return *this; }
    Path parent()
    {
        auto p = _paths;
        p.pop_back();

        return Path(p);
    }
    bool isRoot() const { return _paths.isEmpty(); }
    QString path() const { return "/" + _paths.join('/'); }
private:
    QStringList _paths;
};

class LxMemo : public QWidget
{
    Q_OBJECT

public:
    LxMemo(QWidget* parent = nullptr);
    ~LxMemo();

    void loadMeta(SharedMeta meta, const QString& type);
    void loadRecyleMemo(SharedMeta memo);
    void showMemo(SharedMemo memo);

    sqlite3* db = nullptr;

signals:
    void ReDisplay();
public slots:
    void onAddMemo();
    void onExchangePanel();
    void onWindowClosed();
private slots:
    void onEditMemo(const QModelIndex&);
    void onRecycleItemEdit(const QModelIndex&);
    void onDeleteMemo(const QPoint& pos);
    void onItemEdit(const QModelIndex& index);
    void onUpdateMemo(uint32_t id);
    void onMemoClosed();

    void onBack();
    void onForward();
    void onAddFolder();

    void onEditFinished(uint32_t id, const QString& last, const QString& curr);
protected:
    virtual bool eventFilter(QObject* object, QEvent* event) override;
private:
    void initDB();
    void initRecycleMemo();
    void editItem(const QModelIndex& index, const QPoint& pos);
    void editRecycleItem(const QModelIndex& index);
    void addMemo(SharedMeta memo);
    void queryData(const Path& path);

    void exportHtml(SharedMemo memo);
private:
    Ui::LxMemoClass ui;

    //QGraphicsOpacityEffect* _effect;
    //QPropertyAnimation* _opacityAni;
    QSystemTrayIcon* _trayIcon;

    QMap<uint32_t, QSharedPointer<LucencyDialog>> _opendDlg;
    //LucencyDialog* fontDlg;
    ColorChunk* _cc;
    QListWidgetItem* _currItem{ nullptr };

    bool _startSlide{ false };
    QPoint _slidePos;

    Path _path{ "/" };

    QModelIndex _cutItem;
    SharedMeta _cutMeta = nullptr;
};
